﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cn.sharesdk.unity3d;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.IO;

public class ShareManager : MonoBehaviour
{
    private ShareSDK shareSdk;
    private string requestText = string.Empty;
    public static bool canUpdateShareContent;

    // Start is called before the first frame update
    void Start()
    {
        if (canUpdateShareContent)
        {
            StartCoroutine(GetTxt("https://maxshadow-1305646487.cos.ap-beijing.myqcloud.com/Txt/ShareInfo.txt"));
        }
        else
        {
            if (Application.platform == RuntimePlatform.WindowsEditor ||
                        Application.platform == RuntimePlatform.WindowsPlayer)
                requestText = File.ReadAllText(Application.streamingAssetsPath + "/ShareInfo.txt");
            else if (Application.platform == RuntimePlatform.Android)
                requestText = File.ReadAllText(Application.persistentDataPath + "/ShareInfo.txt");
        }

        shareSdk = GetComponent<ShareSDK>();
        shareSdk.shareHandler = ShareResultHandler;
    }

    private IEnumerator GetTxt(string url)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        yield return request.SendWebRequest();

        if (request.downloadHandler.isDone)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor ||
                        Application.platform == RuntimePlatform.WindowsPlayer)
            {
                File.WriteAllText(Application.streamingAssetsPath + "/ShareInfo.txt", request.downloadHandler.text);
                requestText = File.ReadAllText(Application.streamingAssetsPath + "/ShareInfo.txt");
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                File.WriteAllText(Application.persistentDataPath + "/ShareInfo.txt", request.downloadHandler.text);
                requestText = File.ReadAllText(Application.persistentDataPath + "/ShareInfo.txt");
            }
        }
    }

    public void Share(string type)
    {
        ShareContent content = new ShareContent();

        string[] contents = requestText.Split('\n');
        if (type == "Text")
        {
            foreach (var item in contents)
            {
                string[] oneRow = item.Split('=');
                switch (oneRow[0])
                {
                    case "Text":
                        content.SetText(oneRow[1]);
                        content.SetShareType(ContentType.Text);
                        break;
                }
            }
        }
        else if (type == "Image")
        {
            foreach (var item in contents)
            {
                string[] oneRow = item.Split('=');
                switch (oneRow[0])
                {
                    case "ImageUrl":
                        content.SetImageUrl(oneRow[1]);
                        content.SetShareType(ContentType.Image);
                        break;
                }
            }
        }

        //content.SetShareType(ContentType.Image);
        //shareSdk.ShowShareContentEditor(PlatformType.QQ, content);
        shareSdk.ShowPlatformList(null, content, 100, 100);
        //分享到QQ暂时有些问题，需要在QQ互联内创建应用
        //shareSdk.ShareContent(PlatformType.QQ, content);
    }

    //以下为回调的定义:
    private void ShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        AndroidJavaObject javaObject = new AndroidJavaObject("com.example.commercialpromotion.UnityToAndroid");
        switch (state)
        {
            case ResponseState.Success:
                print("分享成功" + MiniJSON.jsonEncode(result));
                javaObject.Call<bool>("showToast", "分享成功");
                break;
            case ResponseState.Fail:
                print("分享失败");
                javaObject.Call<bool>("showToast", "分享失败");
                break;
            case ResponseState.Cancel:
                print("分享取消");
                javaObject.Call<bool>("showToast", "分享取消");
                break;
        }
    }
}
